<?php
namespace Cyfy;
    
/**
 * Loads the template.
 *
 * Loads the template and puts together given data and shows them as HTML code.
 *
 * @category cyfy
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */
class Template
{
    /**
     * Folder with the template.
     * @var string
     */
    static private $code = 'cyfy';
    
    /**
     * Title of the webpage.
     * @var string
     */
    static private $title = 'Cyfy';
    
    /**
     * Title of the webpage - without server name.
     * @var string
     */
    static private $pageHeader = 'Cyfy';
    
    /**
     * Base of the title, eg. server name. Use %s for inserting the changeable part.
     * @var string
     */
    static private $titleBase = '%s';
    
    /**
     * Filename with template.
     * @var string
     */
    static private $file = '';
    
    /**
     * A HTML code inserted into HEAD element
     * @var string
     */
    static private $head = '';
    
    /**
     * A HTML code inserted just before end of BODY element
     * @var string
     */
    static private $foot = '';

    /**
     * Messages. Used only for inserting data from session to template file/
     * @var string
     */
    static private $messages = '';

    /**
     * HTML blocks, which contains all the blocks.
     * @var array
     */
    static private $places = array();
    
    /**
     * Path to the loaded template. Used for inserting links to the template file.
     * @var string
     */
    static public $url = '';

    /**
     * Preparation
     * @param string Code (path) of the template.
     */
    static function load($code)
    {
        self :: $url = \Cyfy\Cyfy ::  getSourcePath(\Cyfy\Cyfy ::  $templatesDir . $code . "/");
        self :: $code = $code;
        self :: $file = \Cyfy\Cyfy ::  $templatesDir . self :: $code . "/base.tpl.php";
    }
    
    /**
     * Sets the title of the page
     *
     * @param string Title
     */    
    static function setTitle($title)
    {
        self :: $title = $title;
    }    
    
    /**
     * Returns the title
     * @return string Title
     */    
    static function getTitle()
    {
        return self :: $title;
    }    
    
    /**
     * Sets the title base of the page.
     *
     * @param string Title base, the string must contain %s.
     */    
    static function setTitleBase($titleBase)
    {
        self :: $titleBase = $titleBase;
    }    
    
    /**
     * Adds a line to HEAD element.
     *
     * @see \Cyfy\Cyfy :: CSS()
     * @see \Cyfy\Cyfy :: JavaScript()
     * @param string HTML content
     */    
    static function addHeader($content){
        self :: $head .= $content . "\n";
    }
    
    /**
     * Adds a line just before end of BODY element.
     *
     * @see \Cyfy\Cyfy :: CSS()
     * @see \Cyfy\Cyfy :: JavaScript()
     * @param string $content    HTML content
     */    
    static function addFooter($content){
        self :: $foot .= $content . "\n";
    }
    
    
    /**
     * Returns HTML content. Used in template files.
     *
     * @param string Place name
     * @return string
     */    
    static function getPlace($name)
    {
        return "<div class=\"place\" id=\"place-".$name."\">" . @self :: $places[$name] . "</div>";
    }
    
    /**
     * Selects custom template file for rendering.
     *
     * The file can be stored in:
     * <ul>
     *    <li>PRIMARY: templates/Code/$file.php</li>
     *    <li>SECONDARY: modules/ModuleName/templates/$file.php (only if called with module parameter)</li>
     * </ul>
     * If not found, default template file will be used. 
     *
     * @param string Filename without .php
     * @param string Module name
     * @return bool TRUE, if the file was successfuly loaded, FALSE otherwise.
     */ 
    static function useFile($file, $module = '')
    {
        // checks templatesdir first
        $path = \Cyfy\Cyfy ::  $templatesDir . self :: $code . "/" . $file . ".php";
        if(file_exists($path))
        {
            self :: $file = $path;
            return true;
        }
        // if not found, checks modules dir
        elseif($module != "")
        {
            $path = \Cyfy\Cyfy ::  $modulesDir . $module . "/templates/" . $file . ".php";
            if(file_exists($path))
            {
                self :: $file = $path;
                return true;
            }
        }
        // if not found, returns false and does nothing
        return false;
    }
    
    
    /**
     * Builds the template from sources.
     */
    static function build()
    {
        // Loads blocks
        $result = \dibi :: query("SELECT [place], [module], [block] FROM [:cyfy:Templates-blocks] WHERE template = %s ORDER BY [weight] ASC", self :: $code);
        foreach($result as $bt)
        {
            if(\Cyfy\Modules :: isEnabled($bt["module"]))
            {
                self :: $places[$bt["place"]] .= \Cyfy\Blocks :: getBlock($bt["module"], $bt["block"]);
            }
        }
        
        // Sets the title
        self :: $pageHeader = self :: $title;
        self :: $title = sprintf(self :: $titleBase, self :: $title);
        
        // Shows messages and empties cache
        self :: $messages = "<div id=\"cyfy-messages\"></div><div id=\"cyfy-messages-cache\">" . $_SESSION["templateMessages"] . "</div>";
        unset($_SESSION["templateMessages"]);
        
        
        // Includes the template file
        include(self :: $file);
    }
}

